package com.g2room.driving.grap.life;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.g2room.driving.grap.GenericGrap;
import com.g2room.driving.grap.life.model.po.LifeImgs;
import com.g2room.myuall.grap.ky.model.po.GrapErr;
import com.g2room.myuall.util.DbUtil;

public class GrabCtripImgs implements Runnable {
	public static String baseUrl = "http://you.ctrip.com";
	public static final String imgListUrl = "http://you.ctrip.com/DestinationSite/TTDSecond/Photo/AjaxPhotoList?p=#p#&type=#t#&resource=#r#&writing=&rand=0.#rand#&districtId=158";
	public static final String imgDetailUrl = "http://you.ctrip.com/Destinationsite/TTDSecond/Photo/AjaxPhotoDetailList?districtId=158&type=#t#&pindex=1&phsid=#p#&resource=#r#";
	private static String source = "携程";
	private Map<Integer, String> ids = new HashMap<Integer, String>();
	private String type = null;
	private String typeName = null;
	@Override
	public void run() {
		if(ids == null) return;
		for(int id : ids.keySet()) {
			getImgList(type, typeName, id, ids.get(id));
		}
	}
	public GrabCtripImgs(String type, String typeName) {
		this.type= type;
		this.typeName = typeName;
	}
	
	public static void main(String[] args) throws Exception{
		Map<String, String> types = new HashMap<String, String>();
		types.put("2", "美景"); // 美景
		types.put("3", "美食"); // 美食
		types.put("5", "购物"); // 美食
		//#p#&type=#t#&resource=#r#&writing=&rand=0.#rand#
		for(String t : types.keySet()) {
			Map<Integer, String> ids = getIdList(t);
			Map<Integer, String> tids = new HashMap<Integer, String>();
			for(int id : ids.keySet()) {
				tids.put(id,  ids.get(id));
				if(tids.size() == 100) {
					GrabCtripImgs gkd = new GrabCtripImgs(t, types.get(t));
					gkd.ids.putAll(tids);
					Thread thread = new Thread(gkd);
					thread.start();
					tids.clear();
				}
			}
			
			if(tids.size() > 0) {
				GrabCtripImgs gkd = new GrabCtripImgs(t, types.get(t));
				gkd.ids.putAll(tids);
				Thread thread = new Thread(gkd);
				thread.start();
				tids.clear();
			}
			
//			for(int i = 0; i<ids.size(); i++) {
//				tids.put(tids., tids.get(i));
//				GrabCtripImgs gkd = new GrabCtripImgs(type, types.get(type), ids.);
//				
//			}
			
//			for(int id : ids.keySet()) {
//				gkd.getImgList(type, types.get(type), id, ids.get(id));
//			}
		}
    }
	
	public void getImgList(String type, String typeName, int id, String oldId) {
		for(int i=1; i< 101; i++) {
			try {
				System.out.print("\n开始抓取:" + typeName + ",id="+id +",oldid=" + oldId);
				String url = imgListUrl.replace("#p#", i+"").replace("#t#", type).replace("#r#", oldId).replace("#rand#", new Date().getTime()+"");
				if(!getImgs(url, typeName, id, type, oldId)) {
					System.out.println("抓取完毕");
					break;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public boolean getImgs(String url, String tn, int id, String type, String oldId) {
		Document doc = null;
		try {
			doc = GenericGrap.getDocumentByIp(url);
		} catch (IOException e) {
			GrapErr ge = new GrapErr();
			ge.setUrl(url);
			ge.setType("1");
			ge.setContent(id+"");
			ge.setDescription("获取Imgs错误：" + e.getMessage());
			insert2ErrDb(ge);
			return true;
		}

		if (null == doc) return true;
		try {
			Elements els = doc.select("a.itempic");
			if(null == els || els.isEmpty()) return false;
			LifeImgs li = new LifeImgs();
			li.setCategoryName(tn);
			li.setSource(source);
			li.setTargetId(id);
			if(null != els && !els.isEmpty()) {
				if(els.size() <= 40) {
//					li.setSourceUrl(baseUrl + el.attr("data-id"));
					li.setSourceUrl(imgDetailUrl.replace("#p#", els.first().attr("data-id")).replace("#t#", type).replace("#r#", oldId));
					getImg(li);
				} else {
					for(int i=0; i<els.size(); i+=40) {
	//					li.setSourceUrl(baseUrl + el.attr("data-id"));
						li.setSourceUrl(imgDetailUrl.replace("#p#", els.get(i).attr("data-id")).replace("#t#", type).replace("#r#", oldId));
						// you.ctrip.com/Destinationsite/TTDSecond/Photo/AjaxPhotoDetailList?districtId=158&type=3&pindex=1&phsid=16981657&resource=5434646
						getImg(li);
					}
				}
			}
			return true;
		} catch (Exception e) {
			GrapErr ge = new GrapErr();
			ge.setUrl(url);
			ge.setType("2");
			ge.setContent(id+"");
			ge.setDescription("获取List错误：" + e.getMessage());
			insert2ErrDb(ge);
			return true;
		}
	}
	
	public void getImg(LifeImgs tli) {
		String doc = null;
		try {
			doc = GenericGrap.getJsonContent(tli.getSourceUrl());
		} catch (IOException e) {
			GrapErr ge = new GrapErr();
			ge.setUrl(tli.getSourceUrl());
			ge.setType("3");
			ge.setContent(JSON.toJSONString(tli));
			ge.setDescription("获取Img错误：" + e.getMessage());
			insert2ErrDb(ge);
			return;
		}

		if (null == doc) return;
		try {
//			List<LifeImgs> lis =new ArrayList<LifeImgs>();
			List<LifeImgs> lis = JSON.parseObject(doc, new TypeReference<List<LifeImgs>>(){});
			if(null == lis || lis.isEmpty()) return;
			for(LifeImgs li : lis) {
				li.setId(null);
				li.setTargetId(tli.getTargetId());
				li.setCategoryName(tli.getCategoryName());
//				li.setBigImgUrl(tli.getBigImgUrl());
//				li.setSmallImgUrl(tli.getSmallImgUrl());
//				li.setWidth(tli.getWidth());
//				li.setHeight(tli.getHeight());
			}
			
			if(lis.size() > 0) insert2ImgDb(lis);
		} catch (Exception e) {
			GrapErr ge = new GrapErr();
			ge.setUrl(tli.getSourceUrl());
			ge.setType("4");
			ge.setContent(JSON.toJSONString(tli));
			ge.setDescription("获取Img错误：" + e.getMessage());
			insert2ErrDb(ge);
			return;
		}
	}
	
	
	public void insert2ImgDb(List<LifeImgs> lis) {
		String sql = "insert into life_imgs(target_id, category_name,small_img,big_img,width,height) values(?,?,?,?,?,?)";
		PreparedStatement ps = null;
		try {
			ps = DbUtil.getComConn().prepareStatement(sql);
			for(LifeImgs ls : lis) {
				ps.setInt(1, null == ls.getTargetId() ? 0 : ls.getTargetId());
				ps.setString(2, ls.getCategoryName());
				ps.setString(3, ls.getSmallImgUrl());
				ps.setString(4, ls.getBigImgUrl());
				ps.setInt(5, null == ls.getWidth() ? 0 : ls.getWidth());
				ps.setInt(6, null == ls.getHeight() ? 0 : ls.getHeight());
				ps.addBatch();
			}
			ps.executeBatch();	
		} catch (Exception e) {
			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setType("5");
			ge.setContent(JSON.toJSONString(lis));
			ge.setDescription("插入Detail错误："+e.getMessage());
			insert2ErrDb(ge);
			return;
		} finally {
			if(null != ps) {
				try {
					ps.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public void insert2ErrDb(GrapErr ge) {
		PreparedStatement grapPs = null;
		try {
			grapPs = DbUtil.getComConn().prepareStatement("insert into grap_err(description,url,content,type) values(?,?,?,?)");
			grapPs.setString(1, ge.getDescription());
			grapPs.setString(2, ge.getUrl());
			grapPs.setString(3, ge.getContent());
			grapPs.setString(4, ge.getType());
			grapPs.execute();
			grapPs.close();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(null != grapPs) {
				try {
					grapPs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	public static List<GrapErr> getErrs() {
		List<GrapErr> univs = new ArrayList<GrapErr>();
		try {
			Connection connection = DbUtil.getComConn();
			Statement statement = connection.createStatement();
			String sql = "select * from grap_err";
			ResultSet rs = statement.executeQuery(sql);
			while(rs.next()) {
				GrapErr univ = new GrapErr();
				univ.setId(rs.getInt("id"));
				univ.setUrl(rs.getString("url"));
				univ.setType(rs.getString("type"));
				univ.setContent(rs.getString("content"));
				univs.add(univ);
			}
			rs.close();
			statement.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
		return univs;
	}
	
	public static void delErr(int id) {
		PreparedStatement grapPs = null;
		try {
			grapPs = DbUtil.getComConn().prepareStatement("delete from grap_err where id=?");
			grapPs.setInt(1, id);
			grapPs.execute();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(null != grapPs) {
				try {
					grapPs.close();
				} catch (SQLException e) {
					
				}
			}
		}
	}
	
	public static Map<Integer, String> getIdList(String type) {
		Map<Integer, String> map =new HashMap<Integer, String>();
		try {
			Connection connection = DbUtil.getComConn();
			Statement statement = connection.createStatement();
			String sql = "select  id,old_id from life_scenery";
			if(type.equals("2")) {
				sql = "select  id,old_id from life_scenery";
			} else if(type.equals("3")) {
				sql = "select  id,old_id from com_shop where kind_name='餐馆美食' or kind_name='餐馆小吃'";
			} else if(type.equals("5")) {
				sql = "select  id,old_id from com_shop where kind_name='购物'";
			}
			
			ResultSet rs = statement.executeQuery(sql);
			while(rs.next()) {
				map.put(rs.getInt("id") ,rs.getString("old_id"));
			}
			rs.close();
			statement.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
		return map;
	}
	
}
